// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods, numericLiterals, com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString
/**
 * Need to remove JcomboBox of dates
 * Need to figure out selected
 */

package edu.gatech.oad.project.presentation;


import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import edu.gatech.oad.project.networking.clientSessions.PatientSessionClient;
import edu.gatech.oad.project.operations.data.Appointment;




/**
 * This class is a panel that allows the user
 * to interact with the GUI to cancel an
 * existing appointment.
 * 
 * @author Group 17
 * @version 1
 */
public class CancelApp extends JPanel {
  
  /**
   * Field driver: holds the Driver connected to the GUI
   */
  private final Driver driver;
  
  /**
   * Field lblCancelAppointment: label
   */
  private final JLabel lblCancelAppointment;
  
  /**
   * Field lblDate: label
   */
  private final JLabel lblDate;
  
  /**
   * Field comboBox: options for dates
   */
  private final JComboBox comboBox;
  
  /**
   * Field list: list of appointments for a given date
   */
  private final JList list;
  
  /**
   * Field btnCancel: cancel an appointment button
   */
  public final JButton btnCancel;
  
  /**
   * Field btnNewButton: back button
   */
  private final JButton btnNewButton;
  
  /**
   * Field date: holds appointment date
   */
  private int date;

 /**
  * Constructor.
  * 
  * @param driver the Driver connected to the GUI
  */
 public CancelApp(Driver driver) {
   
   this.driver = driver;
  final GridBagLayout gridBagLayout = new GridBagLayout();
  gridBagLayout.columnWidths = new int[]{0, 0, 0};
  gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 67, 0, 0, 0};
  gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0};
  gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 
          0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
  setLayout(gridBagLayout);
  
  lblCancelAppointment = new JLabel("Cancel Appointment");
  lblCancelAppointment.setFont(new Font("Lucida Grande", Font.PLAIN, 30));
  final GridBagConstraints gbc_lblCancelAppointment = new GridBagConstraints();
  gbc_lblCancelAppointment.insets = new Insets(0, 0, 5, 0);
  gbc_lblCancelAppointment.gridwidth = 5;
  gbc_lblCancelAppointment.gridx = 2;
  gbc_lblCancelAppointment.gridy = 1;
  add(lblCancelAppointment, gbc_lblCancelAppointment);
  
  lblDate = new JLabel("Date");
  final GridBagConstraints gbc_lblDate = new GridBagConstraints();
  gbc_lblDate.insets = new Insets(0, 0, 5, 5);
  gbc_lblDate.anchor = GridBagConstraints.EAST;
  gbc_lblDate.gridx = 1;
  gbc_lblDate.gridy = 2;
  add(lblDate, gbc_lblDate);
  
  final String[] dates = {"May 1", "May 2", "May 3", "May 4", "May 5"};
  comboBox = new JComboBox(dates);
  comboBox.addActionListener(new ComboBoxListener(this));
  final GridBagConstraints gbc_comboBox = new GridBagConstraints();
  gbc_comboBox.insets = new Insets(0, 0, 5, 5);
  gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
  gbc_comboBox.gridx = 2;
  gbc_comboBox.gridy = 2;
  add(comboBox, gbc_comboBox);
  
  // need a list of appointments
  // List<Appointment> variable = this.driver.getAppointmentByPatient();
  list = new JList();
  final GridBagConstraints gbc_list = new GridBagConstraints();
  gbc_list.gridwidth = 2;
  gbc_list.gridheight = 3;
  gbc_list.insets = new Insets(0, 0, 5, 5);
  gbc_list.fill = GridBagConstraints.BOTH;
  gbc_list.gridx = 2;
  gbc_list.gridy = 3;
  add(list, gbc_list);
  
  btnCancel = new JButton("Cancel");
  btnCancel.addActionListener(new ButtonListener1(this));
  final GridBagConstraints gbc_btnCancel = new GridBagConstraints();
  gbc_btnCancel.insets = new Insets(0, 0, 5, 5);
  gbc_btnCancel.gridx = 2;
  gbc_btnCancel.gridy = 6;
  add(btnCancel, gbc_btnCancel);
  
  btnNewButton = new JButton("Back to Home");
  btnNewButton.addActionListener(new ButtonListener2(this));
  final GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
  gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
  gbc_btnNewButton.gridx = 3;
  gbc_btnNewButton.gridy = 6;
  add(btnNewButton, gbc_btnNewButton);
 } // end constructor method
 
 /**
  * Get the Driver.
  * 
  * @return the Driver connected to the GUI
  */
 public Driver getDriver() {
   return this.driver;
 } // end getDriver method
 
 /**
  * Get the appointment date.
  * 
  * @return the int representing the appointment date
  */
 public int getDate() {
   return this.date;
 } // end getDate method
 
 /**
  * Set the appointment date.
  * 
  * @param date the int representing the new appointment date
  */
 public void setDate(int date) {
   this.date = date;
 } // end setDate method
 
 /**
  * This private inner class implements ActionListener for comboBox.
  * 
  * @author Group 17
  * @version 1
  */
 private class ComboBoxListener implements ActionListener {
   /**
    * Field cancelApp: the CancelApp being used.
    */
   private final CancelApp cancelApp;
   
   /**
    * Constructor.
    * 
    * @param cancelApp the CancelApp being used
    */
   private ComboBoxListener(CancelApp cancelApp) {
     this.cancelApp = cancelApp;
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e) {
     final String date = (String) ((JComboBox) e.getSource()).getSelectedItem();
     if(date.equals("May 1")) {
       this.cancelApp.setDate(1);
     }
     else if(date.equals("May 2")) {
       this.cancelApp.setDate(2);
     }
     else if(date.equals("May 3")) {
       this.cancelApp.setDate(3);
     }
     else if(date.equals("May 4")) {
       this.cancelApp.setDate(4);
     }
     else if(date.equals("May 5")) {
       this.cancelApp.setDate(5);
     }
     //write code to get list of appointments based on date, then update JList.
   } // end actionPerformed method
 } // end ComboBoxListener class
 
 /**
  * This private inner class implements ActionListener for btnCancel.
  * 
  * @author Group 17
  * @version 1
  */
private class ButtonListener1 implements ActionListener {
  /**
   * Field cancelApp: the CancelApp being used
   */
  private final CancelApp cancelApp;
  
  /**
   * Field driver: the Driver connected to the GUI
   */
  private final Driver driver;
  
  /**
   * Constructor.
   * 
   * @param cancelApp the CancelApp being used
   */
  private ButtonListener1(CancelApp cancelApp) {
    this.cancelApp = cancelApp;
    this.driver = cancelApp.getDriver();
  } // end constructor method
  
  /**
   * Default method for ActionListener.
   * 
   * @param e the ActionEvent triggering the ActionListener
   */
  public void actionPerformed(ActionEvent e) {
    final String app = (String) list.getSelectedValue(); // $codepro.audit.disable variableUsage
    ((PatientSessionClient) this.driver.getSession()).deleteAppointment(
            this.cancelApp.getSelected());
    this.cancelApp.btnCancel.setText("Canceled.");
  } // end actionPerformed method
} // end ButtonListener1 class

/**
 * This private inner class implements ActionListener for btnNewButton.
 * 
 * @author Group 17
 * @version 1
 */
private class ButtonListener2 implements ActionListener {
  /**
   * Field cancelApp: the CancelApp being used
   */
  private final CancelApp cancelApp;
  
  /**
   * Field driver: the Driver connected to the GUI
   */
  private final Driver driver;
  
  /**
   * Constructor.
   * 
   * @param cancelApp the CancelApp being used
   */
  private ButtonListener2(CancelApp cancelApp) {
    this.cancelApp = cancelApp;
    this.driver = cancelApp.getDriver();
  } // end constructor method
  
  /**
   * Default method for ActionListener.
   * 
   * @param e the ActionEvent triggering the ActionListener
   */
  public void actionPerformed(ActionEvent e) {
    this.driver.getFrame().setContentPane(new PatientHome(this.driver));
    this.driver.getFrame().pack();
  } // end actionPerformed method
} // end ButtonListener2 class

/**
 * Get the selected appointmnet
 * 
 * @return the appointment
 */
public Appointment getSelected() {
    // TODO Auto-generated method stub
    return null;
}
} // end CancelApp class